(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:MemberCtrl
     * @description
     * # MemberCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('MemberCtrl', MemberCtrl);

    MemberCtrl.$inject = ['$scope', 'dataService', '$location', 'helper'];

    function MemberCtrl($scope, dataService, $location, helper) {

        $scope.listAction = function() {
            $scope.page_Size = 10;
            $scope.level_id = 0;
            $scope.getlist(1);
        }

        $scope.getlist = function(pageNo, member_id) {
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.page_Size,
                query: {},
                order_by: "member_id:desc"
            };

            // if (member_id) {
            //     query.member_id = $scope.member_id;
            // }

            var subquery = {};
            if ($scope.name) {
                subquery = {
                    "name.icontains": $scope.name
                };
            }

            if ($scope.remark) {
                subquery = {
                    "remark.icontains": $scope.remark
                };
            }

            if (member_id) {
                subquery.member_id = $scope.member_id;
            }

            if ($scope.wxappid) {
                subquery.app_id = $scope.wxappid;
            }

            if ($scope.level_id) {
                subquery.level = $scope.level_id;
            }

            query.query = subquery;
            // console.log("query", query, $scope.wxappid);
            dataService.memberGet(query, true).then(function(list) {
                if (list.wx_members) {
                    if (member_id) {
                        $scope.member = list.wx_members[0];
                    } else {
                        $scope.users = list.wx_members;
                        $scope.total_count = list.total_count;
                    }
                }
            });
        }

        //更新会员等级
        $scope.upLevel = function(member_level_id, member_id) {
            var data = {
                "member_id": member_id,
                "level": member_level_id
            };
            dataService.phpMemberlevelUpdate(data).then(function(result) {
                if (result.Msg != "ok") {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                } else {
                    helper.message("修改成功！");
                }
            });
        }

        //回调方法
        $scope.wxaacallback = function(id) {
            if (id) {
                $scope.wxappid = id;
                $scope.getlist();
            }
        }
        $scope.deleteAction = function() {

        }
        $scope.delMember = function() {
            helper.myConfirm("确认删除此用户的所有消息吗？", function() {
                var query = {
                    open_id: $scope.open_id
                };
                dataService.deleteMember(query).then(function(result) {
                    if (result.Msg) {
                        helper.myAlert("error：" + result.Msg);
                    } else {
                        helper.message("删除成功！");
                    }
                });
            })
        }
        $scope.accountAction = function(pageNo) {
            $scope.member_id = $location.search().member_id;
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.page_Size,
                query: {
                    member_id: $scope.member_id
                }
            };
            dataService.memberAccountGet(query).then(function(list) {
                if (list.accounts) {
                    $scope.member = list.accounts[0];
                    $scope.total_count = list.total_count;
                }
            });
        }

        $scope.infoAction = function(pageNo) {
            $scope.member_id = $location.search().member_id;
            $scope.getlist(1, $scope.member_id);
        }

        //添加数据 初始化
        $scope.addAction = function() {
            var member_id = $scope.member_id = $location.search().member_id;
            console.error("member_id", member_id);

            if (member_id) {
                $scope.getlist(1, member_id);
            }
        }

        $scope.upMemberInfo = function() {
            var data = {
                "member_id": $scope.member_id,
                "phone": $scope.member.phone,
                "remark": $scope.member.remark,
            };
            dataService.memberUpdate(data).then(function(result) {
                if (result.member_id) {
                    helper.myAlert("success", "#/member/list");
                } else {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                }
            });
        }
    }
})();